package com.example.datedemo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.datedemo.domain.WeekSample;
import com.example.datedemo.mapper.WeekSampleMapper;
import com.example.datedemo.service.dto.ResponseResult;
import com.example.datedemo.service.dto.weekSample.WeekSampleAddDto;
import com.example.datedemo.service.dto.weekSample.WeekSampleUpdateDto;
import com.example.datedemo.service.tools.StaticTools;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.example.datedemo.service.tools.StaticTools.getModifyResult;

@Service
public class WeekSampleService {
    @Resource
    private WeekSampleMapper weekSampleMapper;

    // 显示所有Week
    public List<WeekSample> selectAll() {
        return weekSampleMapper.selectList(null);
    }

    // 根据id显示Week
    public ResponseResult selectById(String id) {
        if (id == null || id == "") {
            return new ResponseResult(2, "null");
        }
            WeekSample entity = weekSampleMapper.selectById(id);
            if (entity == null){
                return new ResponseResult(2,"没有该对象");
            }
            return new ResponseResult(1, "成功", entity);
    }

    // 根据projectId,monthNumber,weekNumber显示week
    public ResponseResult selectByWeekNumber(int projectId, String monthNumber, int weekNumber) {
        if (weekNumber == 0 || monthNumber == null) {
            return new ResponseResult(2, "null");
        }

        QueryWrapper<WeekSample> weekSampleQueryWrapper = new QueryWrapper<>();
        weekSampleQueryWrapper.select().apply("date_format(month_number,'%Y-%m') = {0}", monthNumber).eq("week_number", weekNumber).eq("project_id", projectId);
            WeekSample entity = weekSampleMapper.selectOne(weekSampleQueryWrapper);
            if (entity == null){
                return new ResponseResult(2,"没有该对象");
            }
            return new ResponseResult(1, "成功", entity);
    }

    // 内部调用
    // 根据projectId,monthNumber显示范围内的week
    public List<WeekSample> selectByWeekRange(int projectId, String monthNumber) {

        QueryWrapper<WeekSample> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("project_id", projectId).apply("date_format(month_number,'%Y-%m') = {0}", monthNumber);
        // queryWrapper.select().eq("project_id", projectId).apply("date_format(week_number,'%Y-%m') = {0}", month);

        return weekSampleMapper.selectList(queryWrapper);
    }

    // 新增Week
    public ResponseResult insertWeekSample(WeekSampleAddDto weekSampleAddDto) {
        if (StaticTools.isNormalWeek(weekSampleAddDto.getWeekNumber())) {
            return new ResponseResult(0, "周数不正常");
        }

        WeekSample weekSample = new WeekSample();

        weekSample.setProjectId(weekSampleAddDto.getProjectId());

        weekSample.setMonthNumber(StaticTools.tranDate(weekSampleAddDto.getMonthNumber()));

        weekSample.setWeekNumber(weekSampleAddDto.getWeekNumber());
        weekSample.setWeekText(weekSampleAddDto.getWeekText());
        try {
            return getModifyResult(weekSampleMapper.insert(weekSample), weekSample);
        } catch (DuplicateKeyException e) {
            return new ResponseResult(3, "主键重复，该周数据可能已存在");
        }
    }

    // 更新Week
    public ResponseResult updateWeekSample(WeekSampleUpdateDto weekSampleUpdateDto) {
        if (StaticTools.isNormalWeek(weekSampleUpdateDto.getWeekNumber())) {
            return new ResponseResult(0, "周数不正常");
        }
        WeekSample weekSample = new WeekSample();

        weekSample.setProjectId(weekSampleUpdateDto.getProjectId());
        weekSample.setWeekId(weekSampleUpdateDto.getWeekId());
        weekSample.setMonthNumber(StaticTools.tranDate(weekSampleUpdateDto.getMonthNumber()));

        weekSample.setWeekNumber(weekSampleUpdateDto.getWeekNumber());
        weekSample.setWeekText(weekSampleUpdateDto.getWeekText());
        try{
            return getModifyResult(weekSampleMapper.updateById(weekSample), weekSample);
        }catch (DuplicateKeyException e){
            return new ResponseResult(3,"该周数据已存在或主键重复，请使用第六周作为中转周");
        }
    }


    // 根据id删除Week
    public ResponseResult deleteWeekSampleById(String id) {
        int deleteNum = weekSampleMapper.deleteById(id);
        if (deleteNum == 0) {
            return new ResponseResult(0, "删除失败 或 无此id对象");
        }
        return new ResponseResult(1, "成功");
    }


}
